<?php
require_once('sub.inc.php');


if( $_SERVER['QUERY_STRING'] !== "" ){
	$code = clean($_SERVER['QUERY_STRING']);
	if(preg_match("/(.*)\-/", $code, $ray)){
		$code = $ray[1];
		$preview = true;
	}
	$res = mysqli_query($GLOBALS['con'], "SELECT `url`,`id` FROM `links` WHERE `code` LIKE BINARY '$code'");
	if( $row = mysqli_fetch_array($res) ){
		if($preview){
			print "<html>The URL you are requesting is ";
			if(preg_match("/http:\/\/(.*)\.(.*).org\/wiki\/(.*)/", $row['url'], $ray)){
				print "for the <u>" . langLookup($ray[1]) . "</u> edition of <u>" . ucfirst($ray[2]) . "</u>.  The article is titled [[<a href=\"{$row['url']}\">".cleanUp($ray[3])."</a>]]";
			}else{
				print ": <a href=\"{$row['url']}\">{$row['url']}</a>";
			}

		}else{
			header('Location: '. $row['url'], 307);
		}
		
		$res2 = mysqli_query($GLOBALS['con'], "SELECT `id` from `domains` WHERE `tld`='{$_SERVER['SERVER_NAME']}'");
		if( $row2 = mysqli_fetch_array($res2) ){
			$domain = $row2['id'];
		}else{
			$domain = '0';
		}

		mysqli_query($GLOBALS['con'], "INSERT INTO `logs` (`event`, `relatedid`, `message`, `message2`, `ip`, `domain`) VALUES ('HIT', '{$row['id']}', '{$_SERVER['HTTP_USER_AGENT']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['REMOTE_ADDR']}', '$domain')");
	}else{
		print "Sorry, that's not a valid code.";
	}
}else{
	print "<h1>enwn.net v0.2</h1><br>URL Shortening by the <a href=\"http://en.wikinews.org\">English Wikinews</a> community."; 
?>
<br><br>
<h3>About</h3>
Enwn.net was started by [[<a href="http://en.wikinews.org/wiki/User:ShakataGaNai">User:ShakataGaNai</a>]] for the purpose of posting short links on the English Wikinews twitter, via the exisiting RSS 
feed.  By the suggestion of some of the EnWn users, 
enwn.net became publicly accessible to all.  After more commentary, wmf4.me was registered to make the site more accessible to all the Wikimedia Foundation sites.  
This site is a <a href="http://en.wikipedia.org/wiki/URL_shortener">URL Shortener</a>
for the foundation sites ONLY.  With the exception of a few specific authorized users, no one can create links to sites outside of the WMF.
<br><br>
<h3>Contact Us</h3>
Few ways to get ahold of us:
<ul><li><a href="http://en.wikinews.org/wiki/User_talk:ShakataGaNai">ShakataGaNai on Wikinews</a>
<li><a href="http://en.wikinews.org/wiki/User:ShakataGaNai/Wmf4.me">[[User:ShakataGaNai/Wmf4.me]]</a>
<li>wiki ta konsoletek tod com <- Email
<li><a href="http://twitter.com/wmf4me">Twitter</a>
<li><a href="http://identi.ca/wmf4me">Identi.ca</a>
</ul>

<!--/* OpenX iFrame Tag v2.8.2 */-->
<br><br>
<iframe id='a36cd9a5' name='a36cd9a5' src='http://devtionary.org/open/www/delivery/afr.php?zoneid=3&amp;cb=1482' framespacing='0' frameborder='no' scrolling='no' width='468' height='60'><a href='http://devtionary.org/open/www/delivery/ck.php?n=a2a58d74&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://devtionary.org/open/www/delivery/avw.php?zoneid=3&amp;cb=INSERT_RANDOM_NUMBER_HERE&amp;n=a2a58d74' border='0' alt='' /></a></iframe>


<?php
}


function langLookup($code){
	$lang = array(
		'en'=>'English',
		'sr'=>'Siberian',
		'ja'=>'Japanese',
		'de'=>'German',
		'zh'=>'Chinese'
	);

	if(!is_null($lang[$code])){
		return($lang[$code]);
	}else{
		return(strtoupper($code));
	}
}

function cleanUp($title){
	$in = array(
		"/_/",
	);
	$out = array(
		" ",
	);
	$title = preg_replace($in, $out, $title);
	$title = urldecode($title);
	return $title;
}

?>
